﻿using CSC.Managers;
using JDGLSys.Forms.Pages;
using JDGLSys.Forms.Pages.LoginFormPages;
using System;
using System.Windows;

using Model;

namespace JDGLSys.Forms
{
    /// <summary>
    /// LoginForm.xaml 的交互逻辑
    /// </summary>
    public partial class LoginForm : Window
    {
        //指定一个路径用来存放当前窗体的不同页面
        //Uri uri = new Uri("Pages/LoginFormPages/LoginPage.xaml", UriKind.Relative);
        LoginPage loginPage = new LoginPage();
        RegisterPage registerPage = new RegisterPage();
        ResetPage resetPage = new ResetPage();
        ReadCarPage readCarPage = new ReadCarPage();

        public LoginForm()
        {
            InitializeComponent();
            //默认显示登录页面
            LoginFrame.Content = loginPage;
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            //加载页面
            LoginFrame.Content = loginPage;
            //非空判断
            if (loginPage.Account.Text.Equals("") || loginPage.Password.Password.Equals(""))
            {
                MessageBox.Show("账号或密码为空，请输入后重新登录");
            }else
            {
                //登录验证
                switch (LoginYanzheng())
                {
                    case 0:
                        MessageBox.Show("你还不是我们的用户，请注册后再登录");
                        break;
                    case 1:
                        AdminForm adminForm = new AdminForm();
                        this.Close();
                        adminForm.Show();
                        MessageBox.Show("管理员，你好");
                        NowPerson.Account = loginPage.Account.Text;
                        break;
                    case 2:
                        UserForm userForm = new UserForm();
                        this.Close();
                        userForm.Show();
                        MessageBox.Show("用户，你好");
                        NowPerson.Account = loginPage.Account.Text;
                        break;

                }
            }
                

        }

        private void Register_Click(object sender, RoutedEventArgs e)
        {
            //加载页面
            LoginFrame.Content = registerPage;
        }

        private void ReSetPwd_Click(object sender, RoutedEventArgs e)
        {
            //加载页面
            LoginFrame.Content = resetPage;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //加载页面
            LoginFrame.Content = readCarPage;
        }

        //管理员则返回1，用户则返回2，用户不存在则返回0
        private int LoginYanzheng()
        {
            //是用户还是管理员
            int flag = 0;
            int IsAdmin = AdminsTManager.countByAccount(loginPage.Account.Text, loginPage.Password.Password);
            int IsUser = UsersTManager.countByAccount(loginPage.Account.Text, loginPage.Password.Password);
            if (IsAdmin == 1)
                flag = 1;
            if (IsUser == 1)
                flag = 2;
            return flag;

        }


    }
}
